#!/usr/bin/env python
# coding=utf-8

from vavava.workqueue import Work
from . import fetcher_tnt
from .. import taskworkbase
from .. import fetcherbase

class TntWork(Work):
    def __init__( self, item_id, group, proxy=None, begin_time=None, period=None, log=None ):
        Work.__init__( self, period=period, group=group, begin_time=begin_time,
            log=log, name="[TntWork|%s]" % item_id )
        self.fetcher_ = fetcher_tnt.FetcherTnt ( item_id=item_id, log=log )
        self.proxy_  = proxy

    def do(self,worker=None):
        self.fetcher_.SetDBConn(self._work_param)
        self.fetcher_.SetProxy(self.proxy_)
        self.fetcher_.Fetch()

    def __hash__(self):
        return hash(self.fetcher_.item_id)

class TntTaskWork(taskworkbase.TaskWorkBase):
    def __init__( self,period,group,begin_time=None, end_time=None,log=None,**kwarg ):
        taskworkbase.TaskWorkBase.__init__(
            self,track_type=fetcherbase.TRACK_TYPE_TNT, period=period,
            group=group, begin_time=begin_time, end_time=end_time,log=log )
        if kwarg:
            proxy = kwarg.get("proxy")
            if proxy:
                self.proxy = kwarg["proxy"]

    def _get_all_items(self):
        return get_track_items_from_web(self.log)

    def _get_work_instance(self, item_id):
        return TntWork(
            item_id=item_id,
            group=self.group_id,
            proxy=self.proxy_,
            begin_time=None,
            period=None,
            log=self.log
        )

def get_track_items_from_web(log=None):
    return ["435876560"]


